import api from '../../api';
import { message } from 'antd';
import { forEach } from 'lodash';

export default {
  namespace: 'depositOrderM', //存款指令
  state: {},
  effects: {
    //查询当前资金
    *queryFund({ payload, callback }, { call, put }) {
      const response = yield call(api.stockOrder.queryFund, payload);
      if (response) {
        if (response.success) {
          const data = response.data[0] || {};
          callback('success', data);
        } else {
          callback();
          message.error(response.message || '查询当前资金失败!');
        }
      } else {
        callback();
        message.error('查询当前资金失败!');
      }
    },

    //查询当前持仓
    *getPosition({ payload, callback }, { call, put }) {
      const response = yield call(api.depositOrderApi.getPosition, payload);

      if (response) {
        if (response.code == 0) {
          callback('success', response.data || {});
        } else {
          callback('fail');
          message.error(response.msg || '查询当前持仓失败!');
        }
      } else {
        callback('fail');
        message.error('查询当前持仓失败!');
      }
    },
    //查询系列产品持仓
    *getPositionTrade({ payload, callback }, { call, put }) {
      const response = yield call(api.depositOrderApi.getPositionTrade, payload);

      if (response) {
        if (response.code == 0) {
          callback('success', response.data || {});
        } else {
          callback('fail');
          message.error(response.msg || '查询当前持仓失败!');
        }
      } else {
        callback('fail');
        message.error('查询当前持仓失败!');
      }
    },
    //查询存款总行列表
    *getHeadBankList({ payload, callback }, { call, put }) {
      const response = yield call(api.depositOrderApi.getHeadBankList, payload);
      if (response) {
        if (response.code == 0) {
          const data = response.data || {};
          callback('success', payload.value, data.dataSource || []);
        } else {
          callback('fail');
          message.error(response.msg || '查询存款总行列表失败!');
        }
      } else {
        callback('fail');
        message.error('查询存款总行列表失败!');
      }
    },
    //查询存款分行
    *getBranchBankList({ payload, callback }, { call, put }) {
      const response = yield call(api.depositOrderApi.getBranchBankList, payload);
      if (response) {
        if (response.code == 0) {
          const data = response.data.dataSource || [];
          callback('success', data);
        } else {
          callback('fail');
          message.error(response.msg || '查询存款分行列表失败!');
        }
      } else {
        callback('fail');
        message.error('查询存款分行列表失败!');
      }
    },
    //获取存款利率
    *getRate({ payload, callback }, { call, put }) {
      const response = yield call(api.depositOrderApi.getRate, payload);
      if (response) {
        if (response.code == 0) {
          const data = response.data.dataSource || {};
          callback('success', data.rate);
        } else {
          callback('fail');
          // message.error(response.msg || '查询存款利率失败!');
        }
      } else {
        callback('fail');
        message.error('查询存款利率失败!');
      }
    },
    //指令下达新增
    *orderSave({ payload, callback }, { call, put }) {
      const response = yield call(api.depositOrderApi.orderSave, payload);
      if (response) {
        if (response.success) {
          callback('success');
        } else {
          callback('error', response);
        }
      } else {
        callback('error', {});
      }
    },
    //簿记新增 修改
    *tradeBookSave({ payload, callback }, { call, put }) {
      const response = yield call(api.depositOrderApi.tradeBookSave, payload);
      if (response) {
        if (response.success) {
          callback('success');
        } else {
          callback('error', response);
        }
      } else {
        callback('error', {});
      }
    },
    //查询存款账号
    *getAccountInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.depositOrderApi.getAccountInfo, payload);
      if (response) {
        if (response.code == 0) {
          const data = response.data.dataSource || [];
          callback('success', data);
        } else {
          callback();
          message.error(response.msg || '查询存款账号失败!');
        }
      } else {
        callback();
        message.error('查询存款账号失败!');
      }
    },
    //交易录入
    *tradeAdd({ payload, callback }, { call, put }) {
      const response = yield call(api.depositOrderApi.tradeAdd, payload);
      if (response) {
        if (response.success) {
          callback('success');
        } else {
          callback('error', response);
        }
      } else {
        callback('error', {});
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
